home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevmswn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  14.2 KB  |  486 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevmswn.c,v 1.2 2000/09/19 19:00:14 lpd Exp $ */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript.
  22.  *
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  26.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  27.  */
  28. #include "gdevmswn.h"
  29. #include "gp.h"
  30. #include "gpcheck.h"
  31. #include "gsparam.h"
  32. #include "gdevpccm.h"
  33. #include "gsdll.h"
  34. #include "gsdllwin.h"
  35.  
  36. /* Forward references */
  37. private int win_set_bits_per_pixel(P2(gx_device_win *, int));
  38.  
  39. #define TIMER_ID 1
  40.  
  41. /* Open the win driver */
  42. int
  43. win_open(gx_device * dev)
  44. {
  45.     HDC hdc;
  46.     int code;
  47.  
  48.     if (dev->width == INITIAL_WIDTH)
  49.     dev->width = (int)(8.5 * dev->x_pixels_per_inch);
  50.     if (dev->height == INITIAL_HEIGHT)
  51.     dev->height = (int)(11.0 * dev->y_pixels_per_inch);
  52.  
  53.     if (wdev->BitsPerPixel == 0) {
  54.     int depth;
  55.  
  56.     /* Set parameters that were unknown before opening device */
  57.     /* Find out if the device supports color */
  58.     /* We recognize 1, 4, 8, 16, 24 bit/pixel devices */
  59.     hdc = GetDC(NULL);    /* get hdc for desktop */
  60.     depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
  61.     if (depth > 16) {
  62.         wdev->BitsPerPixel = 24;
  63.     } else if (depth > 8) {
  64.         wdev->BitsPerPixel = 16;
  65.     } else if (depth >= 8) {
  66.         wdev->BitsPerPixel = 8;
  67.     } else if (depth >= 4) {
  68.         wdev->BitsPerPixel = 4;
  69.     } else {
  70.         wdev->BitsPerPixel = 1;
  71.     }
  72.     ReleaseDC(NULL, hdc);
  73.     wdev->mapped_color_flags = 0;
  74.     }
  75.     if ((code = win_set_bits_per_pixel(wdev, wdev->BitsPerPixel)) < 0)
  76.     return code;
  77.  
  78.     if (wdev->nColors > 0) {
  79.     /* create palette for display */
  80.     if ((wdev->limgpalette = win_makepalette(wdev))
  81.         == (LPLOGPALETTE) NULL)
  82.         return win_nomemory();
  83.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  84.     }
  85.     return 0;
  86. }
  87.  
  88. /* Make the output appear on the screen. */
  89. int
  90. win_sync_output(gx_device * dev)
  91. {
  92.     (*pgsdll_callback) (GSDLL_SYNC, (unsigned char *)wdev, 0);
  93.     return (0);
  94. }
  95.  
  96. /* Make the window visible, and display the output. */
  97. int
  98. win_output_page(gx_device * dev, int copies, int flush)
  99. {
  100.     (*pgsdll_callback) (GSDLL_PAGE, (unsigned char *)wdev, 0);
  101.     return gx_finish_output_page(dev, copies, flush);;
  102. }
  103.  
  104. /* Close the win driver */
  105. int
  106. win_close(gx_device * dev)
  107. {
  108.     /* Free resources */
  109.     if (wdev->nColors > 0) {
  110.     gs_free(wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
  111.     DeleteObject(wdev->himgpalette);
  112.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) +
  113.         (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  114.         "win_close");
  115.     }
  116.     return (0);
  117. }
  118.  
  119. /* Map a r-g-b color to the colors available under Windows */
  120. gx_color_index
  121. win_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  122.           gx_color_value b)
  123. {
  124.     switch (wdev->BitsPerPixel) {
  125.     case 24:
  126.         return (((unsigned long)b >> (gx_color_value_bits - 8)) << 16) +
  127.         (((unsigned long)g >> (gx_color_value_bits - 8)) << 8) +
  128.         (((unsigned long)r >> (gx_color_value_bits - 8)));
  129.     case 16:{
  130.         gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 11) +
  131.                        ((g >> (gx_color_value_bits - 6)) << 5) +
  132.                        (b >> (gx_color_value_bits - 5));
  133. #if arch_is_big_endian
  134.         ushort color16 = (ushort)color;
  135. #else
  136.         ushort color16 = (ushort)((color << 8) | (color >> 8));
  137. #endif
  138.         return color16;
  139.         }
  140.     case 15:{
  141.         gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 10) +
  142.                        ((g >> (gx_color_value_bits - 5)) << 5) +
  143.                        (b >> (gx_color_value_bits - 5));
  144. #if arch_is_big_endian
  145.         ushort color15 = (ushort)color;
  146. #else
  147.         ushort color15 = (ushort)((color << 8) | (color >> 8));
  148. #endif
  149.         return color15;
  150.         }
  151.     case 8:{
  152.         int i;
  153.         LPLOGPALETTE lpal = wdev->limgpalette;
  154.         PALETTEENTRY *pep;
  155.         byte cr, cg, cb;
  156.         int mc_index;
  157.         byte mc_mask;
  158.  
  159.         /* Check for a color in the palette of 64. */
  160.         {
  161.             static const byte pal64[32] =
  162.             {
  163.             1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  164.             1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  165.             1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  166.             1
  167.             };
  168.  
  169.             if (pal64[r >> (gx_color_value_bits - 5)] &&
  170.             pal64[g >> (gx_color_value_bits - 5)] &&
  171.             pal64[b >> (gx_color_value_bits - 5)]
  172.             )
  173.             return (gx_color_index) (
  174.                    ((r >> (gx_color_value_bits - 2)) << 4) +
  175.                    ((g >> (gx_color_value_bits - 2)) << 2) +
  176.                         (b >> (gx_color_value_bits - 2))
  177.                 );
  178.         }
  179.  
  180.         /* map colors to 0->255 in 32 steps */
  181.         cr = win_color_value(r);
  182.         cg = win_color_value(g);
  183.         cb = win_color_value(b);
  184.  
  185.         /* Search in palette, skipping the first 64. */
  186.         mc_index = ((cr >> 3) << 7) + ((cg >> 3) << 2) + (cb >> 6);
  187.         mc_mask = 0x80 >> ((cb >> 3) & 7);
  188.         if (wdev->mapped_color_flags[mc_index] & mc_mask)
  189.             for (i = wdev->nColors, pep = &lpal->palPalEntry[i];
  190.              --pep, --i >= 64;
  191.             ) {
  192.             if (cr == pep->peRed &&
  193.                 cg == pep->peGreen &&
  194.                 cb == pep->peBlue
  195.                 )
  196.                 return ((gx_color_index) i);    /* found it */
  197.             }
  198.         /* next try adding it to palette */
  199.         i = wdev->nColors;
  200.         if (i < 220) {    /* allow 36 for windows and other apps */
  201.             LPLOGPALETTE lipal = wdev->limgpalette;
  202.  
  203.             wdev->nColors = i + 1;
  204.  
  205.             DeleteObject(wdev->himgpalette);
  206.             lipal->palPalEntry[i].peFlags = 0;
  207.             lipal->palPalEntry[i].peRed = cr;
  208.             lipal->palPalEntry[i].peGreen = cg;
  209.             lipal->palPalEntry[i].peBlue = cb;
  210.             lipal->palNumEntries = wdev->nColors;
  211.             wdev->himgpalette = CreatePalette(lipal);
  212.  
  213.             wdev->mapped_color_flags[mc_index] |= mc_mask;
  214.             return ((gx_color_index) i);    /* return new palette index */
  215.         }
  216.         return (gx_no_color_index);    /* not found - dither instead */
  217.         }
  218.     case 4:
  219.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  220.         && (r < gx_max_color_value / 4 * 3))
  221.         return ((gx_color_index) 8);    /* light gray */
  222.         return pc_4bit_map_rgb_color(dev, r, g, b);
  223.     }
  224.     return (gx_default_map_rgb_color(dev, r, g, b));
  225. }
  226.  
  227. /* Map a color code to r-g-b. */
  228. int
  229. win_map_color_rgb(gx_device * dev, gx_color_index color,
  230.           gx_color_value prgb[3])
  231. {
  232.     gx_color_value one;
  233.     ushort value;
  234.  
  235.     switch (wdev->BitsPerPixel) {
  236.     case 24:
  237.         one = (gx_color_value) (gx_max_color_value / 255);
  238.         prgb[0] = ((color) & 255) * one;
  239.         prgb[1] = ((color >> 8) & 255) * one;
  240.         prgb[2] = ((color >> 16) & 255) * one;
  241.         break;
  242.     case 16:
  243.         value = (color >> 11) & 0x1f;
  244.         prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  245.         value = (color >> 5) & 0x3f;
  246.         prgb[1] = ((value << 10) + (value << 4) + (value >> 2)) >> (16 - gx_color_value_bits);
  247.         value = (color) & 0x1f;
  248.         prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  249.         break;
  250.     case 15:
  251.         value = (color >> 10) & 0x1f;
  252.         prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  253.         value = (color >> 5) & 0x1f;
  254.         prgb[1] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  255.         value = (color) & 0x1f;
  256.         prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  257.         break;
  258.     case 8:
  259.         if (!dev->is_open)
  260.         return -1;
  261.         one = (gx_color_value) (gx_max_color_value / 255);
  262.         prgb[0] = wdev->limgpalette->palPalEntry[(int)color].peRed * one;
  263.         prgb[1] = wdev->limgpalette->palPalEntry[(int)color].peGreen * one;
  264.         prgb[2] = wdev->limgpalette->palPalEntry[(int)color].peBlue * one;
  265.         break;
  266.     case 4:
  267.         if (color == 8)    /* VGA light gray */
  268.         prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  269.         else
  270.         pc_4bit_map_color_rgb(dev, color, prgb);
  271.         break;
  272.     default:
  273.         prgb[0] = prgb[1] = prgb[2] =
  274.         (int)color ? gx_max_color_value : 0;
  275.     }
  276.     return 0;
  277. }
  278.  
  279. /* Get Win parameters */
  280. int
  281. win_get_params(gx_device * dev, gs_param_list * plist)
  282. {
  283.     int code = gx_default_get_params(dev, plist);
  284.  
  285.     return code;
  286. }
  287.  
  288. /* Put parameters. */
  289. /* Set window parameters -- size and resolution. */
  290. /* We implement this ourselves so that we can do it without */
  291. /* closing and opening the device. */
  292. int
  293. win_put_params(gx_device * dev, gs_param_list * plist)
  294. {
  295.     int ecode = 0, code;
  296.     bool is_open = dev->is_open;
  297.     int width = dev->width;
  298.     int height = dev->height;
  299.     int old_bpp = dev->color_info.depth;
  300.     int bpp = old_bpp;
  301.     byte *old_flags = wdev->mapped_color_flags;
  302.  
  303.     /* Handle extra parameters */
  304.  
  305.     switch (code = param_read_int(plist, "BitsPerPixel", &bpp)) {
  306.     case 0:
  307.         if (dev->is_open && bpp != old_bpp)
  308.         ecode = gs_error_rangecheck;
  309.         else {        /* Don't release existing mapped_color_flags. */
  310.         if (bpp != 8)
  311.             wdev->mapped_color_flags = 0;
  312.         code = win_set_bits_per_pixel(wdev, bpp);
  313.         if (code < 0)
  314.             ecode = code;
  315.         else
  316.             break;
  317.         }
  318.         goto bppe;
  319.     default:
  320.         ecode = code;
  321.       bppe:param_signal_error(plist, "BitsPerPixel", ecode);
  322.     case 1:
  323.         break;
  324.     }
  325.  
  326.     if (ecode >= 0) {        /* Prevent gx_default_put_params from closing the device. */
  327.     dev->is_open = false;
  328.     ecode = gx_default_put_params(dev, plist);
  329.     dev->is_open = is_open;
  330.     }
  331.     if (ecode < 0) {        /* If we allocated mapped_color_flags, release it. */
  332.     if (wdev->mapped_color_flags != 0 && old_flags == 0)
  333.         gs_free(wdev->mapped_color_flags, 4096, 1,
  334.             "win_put_params");
  335.     wdev->mapped_color_flags = old_flags;
  336.     if (bpp != old_bpp)
  337.         win_set_bits_per_pixel(wdev, old_bpp);
  338.     return ecode;
  339.     }
  340.     if (wdev->mapped_color_flags == 0 && old_flags != 0) {    /* Release old mapped_color_flags. */
  341.     gs_free(old_flags, 4096, 1, "win_put_params");
  342.     }
  343.     /* Hand off the change to the implementation. */
  344.     if (is_open && (bpp != old_bpp ||
  345.             dev->width != width || dev->height != height)
  346.     ) {
  347.     int ccode;
  348.  
  349.     (*wdev->free_bitmap) (wdev);
  350.     ccode = (*wdev->alloc_bitmap) (wdev, (gx_device *) wdev);
  351.     if (ccode < 0) {    /* Bad news!  Some of the other device parameters */
  352.         /* may have changed.  We don't handle this. */
  353.         /* This is ****** WRONG ******. */
  354.         dev->width = width;
  355.         dev->height = height;
  356.         win_set_bits_per_pixel(wdev, old_bpp);
  357.         (*wdev->alloc_bitmap) (wdev, dev);
  358.         return ccode;
  359.     }
  360.     }
  361.     return 0;
  362. }
  363.  
  364. /* ------ Internal routines ------ */
  365.  
  366. #undef wdev
  367.  
  368.  
  369.  
  370. /* out of memory error message box */
  371. int
  372. win_nomemory(void)
  373. {
  374.     MessageBox((HWND) NULL, (LPSTR) "Not enough memory", (LPSTR) szAppName, MB_ICONSTOP);
  375.     return gs_error_limitcheck;
  376. }
  377.  
  378.  
  379. LPLOGPALETTE
  380. win_makepalette(gx_device_win * wdev)
  381. {
  382.     int i, val;
  383.     LPLOGPALETTE logpalette;
  384.  
  385.     logpalette = (LPLOGPALETTE) gs_malloc(1, sizeof(LOGPALETTE) +
  386.              (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  387.                       "win_makepalette");
  388.     if (logpalette == (LPLOGPALETTE) NULL)
  389.     return (0);
  390.     logpalette->palVersion = 0x300;
  391.     logpalette->palNumEntries = wdev->nColors;
  392.     for (i = 0; i < wdev->nColors; i++) {
  393.     logpalette->palPalEntry[i].peFlags = 0;
  394.     switch (wdev->nColors) {
  395.         case 64:
  396.         /* colors are rrggbb */
  397.         logpalette->palPalEntry[i].peRed = ((i & 0x30) >> 4) * 85;
  398.         logpalette->palPalEntry[i].peGreen = ((i & 0xC) >> 2) * 85;
  399.         logpalette->palPalEntry[i].peBlue = (i & 3) * 85;
  400.         break;
  401.         case 16:
  402.         /* colors are irgb */
  403.         val = (i & 8 ? 255 : 128);
  404.         logpalette->palPalEntry[i].peRed = i & 4 ? val : 0;
  405.         logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
  406.         logpalette->palPalEntry[i].peBlue = i & 1 ? val : 0;
  407.         if (i == 8) {    /* light gray */
  408.             logpalette->palPalEntry[i].peRed =
  409.             logpalette->palPalEntry[i].peGreen =
  410.             logpalette->palPalEntry[i].peBlue = 192;
  411.         }
  412.         break;
  413.         case 2:
  414.         logpalette->palPalEntry[i].peRed =
  415.             logpalette->palPalEntry[i].peGreen =
  416.             logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
  417.         break;
  418.     }
  419.     }
  420.     return (logpalette);
  421. }
  422.  
  423.  
  424. private int
  425. win_set_bits_per_pixel(gx_device_win * wdev, int bpp)
  426. {
  427.     static const gx_device_color_info win_24bit_color = dci_color(24, 255, 255);
  428.     static const gx_device_color_info win_16bit_color = dci_color(16, 255, 255);
  429.     static const gx_device_color_info win_8bit_color = dci_color(8, 31, 4);
  430.     static const gx_device_color_info win_ega_color = dci_pc_4bit;
  431.     static const gx_device_color_info win_vga_color = dci_pc_4bit;
  432.     static const gx_device_color_info win_mono_color = dci_black_and_white;
  433.     /* remember old anti_alias info */
  434.     gx_device_anti_alias_info anti_alias = wdev->color_info.anti_alias;
  435.     HDC hdc;
  436.  
  437.     switch (bpp) {
  438.     case 24:
  439.         wdev->color_info = win_24bit_color;
  440.         wdev->nColors = -1;
  441.         break;
  442.     case 16:
  443.     case 15:
  444.         wdev->color_info = win_16bit_color;
  445.         wdev->nColors = -1;
  446.         break;
  447.     case 8:
  448.         /* use 64 static colors and 166 dynamic colors from 8 planes */
  449.         wdev->color_info = win_8bit_color;
  450.         wdev->nColors = 64;
  451.         break;
  452.     case 4:
  453.         hdc = GetDC(NULL);
  454.         if (GetDeviceCaps(hdc, VERTRES) <= 350)
  455.         wdev->color_info = win_ega_color;
  456.         else
  457.         wdev->color_info = win_vga_color;
  458.         ReleaseDC(NULL, hdc);
  459.         wdev->nColors = 16;
  460.         break;
  461.     case 1:
  462.         wdev->color_info = win_mono_color;
  463.         wdev->nColors = 2;
  464.         break;
  465.     default:
  466.         return (gs_error_rangecheck);
  467.     }
  468.     wdev->BitsPerPixel = bpp;
  469.  
  470.     /* If necessary, allocate and clear the mapped color flags. */
  471.     if (bpp == 8) {
  472.     if (wdev->mapped_color_flags == 0) {
  473.         wdev->mapped_color_flags = gs_malloc(4096, 1, "win_set_bits_per_pixel");
  474.         if (wdev->mapped_color_flags == 0)
  475.         return_error(gs_error_VMerror);
  476.     }
  477.     memset(wdev->mapped_color_flags, 0, 4096);
  478.     } else {
  479.     gs_free(wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
  480.     wdev->mapped_color_flags = 0;
  481.     }
  482.     /* restore old anti_alias info */
  483.     wdev->color_info.anti_alias = anti_alias;
  484.     return 0;
  485. }
  486.